<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class PostController extends Controller
{
    //列表页
    public function index(Request $request)
    {
        if(isset($request -> id))
        {
            $num = $request -> input('num', 15);
            $data = DB::table('post')
            -> join('userdetail', 'post.uid', '=', 'userdetail.id')
            -> join('types', 'post.pid', '=', 'types.id')
            -> select('post.*', 'userdetail.name','types.name as pname')
            -> whereNotIn('recycle', [1])
            -> whereNotIn('auth', [1])
            -> whereIn('post.pid', [$request -> id])
            -> where(function($query) use ($request){
                $query -> where('content', 'like', '%'.$request -> input('keyword').'%');
            })
            -> orderBy('pid', 'desc')
            -> paginate($num);
            return view('admin.post.index', ['data' => $data, 'request' => $request -> all()]);
        }else
        {
        	$num = $request -> input('num', 15);
        	$data = DB::table('post')
            -> join('userdetail', 'post.uid', '=', 'userdetail.id')
            -> join('types', 'post.pid', '=', 'types.id')
            -> select('post.*', 'userdetail.name','types.name as pname')
            -> whereNotIn('recycle', [1])
            -> whereNotIn('auth', [1])
        		-> where(function($query) use ($request){
        			$query -> where('content', 'like', '%'.$request -> input('keyword').'%');
        		})
        		-> orderBy('pid', 'desc')
        		-> paginate($num);
            return view('admin.post.index', ['data' => $data, 'request' => $request -> all()]);
        }
    }

    //ajaxUpdate
    public function ajaxUpdate(Request $request)
    {
    	$id = $request -> input('id');

    	$data = DB::table('post') -> where('id', $id) -> first();
    	if($data -> recycle == '0')
    	{
    		$res = DB::table('post') ->  where('id', $id) -> update(['recycle' => '1']);
    		if($res)
    		{
    			return response() -> json(0);
    		}else
    		{
    			return response() -> json(1);
    		}

    	}else if($data -> recycle == '1')
    	{
    		$res = DB::table('post') ->  where('id', $id) -> update(['recycle' => '0']);
    		if($res)
    		{
    			return response() -> json(2);
    		}else
    		{
    			return response() -> json(3);
    		}
    	}

    }

   	//回收站
   	public function recycle(Request $request)
   	{
   		$num = $request -> input('num', 15);


    	$data = DB::table('post')
            -> join('userdetail', 'post.uid', '=', 'userdetail.id')
            -> join('types', 'post.pid', '=', 'types.id')
            -> select('post.*', 'userdetail.name','types.name as pname')
            -> whereNotIn('recycle', [0])
    		-> where(function($query) use ($request){
    			$query -> where('content', 'like', '%'.$request -> input('keyword').'%');
    		})
    		-> orderBy('pid', 'desc')
    		-> paginate($num);
        return view('admin.post.recycle', ['data' => $data, 'request' => $request -> all()]);
   	}

   	//删除
   	public function delete($id)
   	{
   		$res = DB::table('reply') -> where('pid', $id) -> first();
   		//判断有无回复
    	if($res)
    	{
    		//先删除回复
    		$rws = DB::table('reply') -> where('pid', $id) -> delete();
    		//判断是否删除成功
    		if($rws)
    		{
    			//再删除影评
	            $rew = DB::table('post') -> where('id', $id) -> delete();
	            if($rew)
	            {
	    			return redirect('/admin/post/recycle') -> with(['info' => '删除成功']);
	            }else
	            {
	                return back() -> with(['info' => '删除失败']);
	            }
	        }else
    		{
    			return back() -> with(['info' => '删除失败']);
    		}
    	}else
    	{
    		//没有回复直接删除
    		$rsw = DB::table('post') -> where('id', $id) -> delete();
            if($rsw)
            {
    		 	return redirect('/admin/post/recycle') -> with(['info' => '删除成功']);
            }else
            {
                return back() -> with(['info' => '删除失败']);
            }
    	}
   	}
}
